﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Security;

namespace Cms.Web.CmsSystem
{
    public partial class Default : PageBase
    {
        public string TreeJson;
        BLL.TabPage bll = new Cms.BLL.TabPage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/CmsSystem/Login.aspx");
            }
            else
            {
                BuildTab();
            }
        }
        public void BuildTab() {
           List<Model.Sys_Tabpage> list = bll.GetAll();
           List<Model.Sys_Tabpage> listChild= bll.GetChilds(list,"");
           StringBuilder sb = new StringBuilder();
           StringBuilder json = new StringBuilder();
           foreach (Model.Sys_Tabpage model in listChild) {
               sb.AppendFormat("<div title='{0}' iconCls='icon {1}'>", model.Title, model.Icon);
               sb.AppendLine("      <div class='layout' fit='true'>");
               sb.AppendFormat("      <div id='{0}' region=\"west\" class='ztree' style='width: 200px;' split='true' ></div>",model.Id);
               sb.AppendFormat("      <div region=\"center\"  style=\"overflow: hidden; border: 0;width:1000px\"><div id='tab_{0}' class='centertab' fit='true'><div title='欢迎'></div></div></div>", model.Id);
               sb.AppendLine("</div></div>");
               json.AppendFormat("'{0}':[{1}],", model.Id, BuildTreeJson(list, model.Id).Trim(System.Environment.NewLine.ToCharArray()).Trim(','));
           }
           maintab.InnerHtml = sb.ToString();
           TreeJson = json.ToString().Trim(',');
        }
        public string GetUserName {
            get {
                return EntLib.FormsPrincipal<Model.Sys_Userinfo>.GetLoginUser().Name;
            }
        }
        private string BuildTreeJson(List<Model.Sys_Tabpage> list, string tabid)
        {
            List<Model.Sys_Tabpage> listChild = bll.GetChilds(list, tabid);
            if (listChild.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Model.Sys_Tabpage model in listChild)
                {
                    sb.Append("{");
                    sb.AppendFormat(" id: '{0}', pId: '{1}', name: '{2}',tabtype:'{3}',iconSkin:'icon {4}'", model.Id, model.Parentid, model.Title, model.Tab_Type, String.IsNullOrEmpty(model.Icon) ? "default" : model.Icon);
                    sb.AppendLine("},");
                    sb.Append(BuildTreeJson(list, model.Id));
                }
                return sb.ToString();
            }
            return "";
        }
        public override void SetPageHeader()
        {
            System.Web.UI.WebControls.Literal li = new Literal();
            StringBuilder sb = new StringBuilder();
            //sb.AppendFormat("<link href=\"../App_Themes/{0}/ui.jqgrid.css\" rel=\"stylesheet\" type=\"text/css\" />",Theme );
            sb.AppendFormat("<link href=\"../App_Themes/{0}/easyui/icon.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n", Theme);
            sb.AppendFormat("<link href=\"../App_Themes/{0}/easyui/easyui.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n", Theme);
            sb.AppendFormat("<link href=\"/App_Themes/{0}/zTreeStyle/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n", Theme);
            sb.AppendFormat("<link href=\"../App_Themes/{0}/default.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n", Theme);
            //li.Text = "<link type=\"text/css\" id=\"logintheme\" rel=\"stylesheet\" href=\"app_themes/" + CurrentTheme + "/login.css\" /> ";
            li.Text = sb.ToString();
            this.BDPageHeader.Controls.Add(li);
            base.SetPageHeader();
            base.SetPageHeader();
        }
    }
}
